package com.zbkj.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zbkj.common.request.PageParamRequest;
import com.github.pagehelper.PageHelper;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zbkj.common.exception.AppException;

import com.zbkj.common.result.CommonResultCode;
import com.zbkj.common.model.agent.UserStoreRelation;
import com.zbkj.service.dao.UserStoreRelationDao;
import com.zbkj.service.service.UserStoreRelationService;

import com.zbkj.common.request.UserStoreRelationSaveRequest;
import com.zbkj.common.request.UserStoreRelationSearchRequest;
import com.zbkj.common.response.UserStoreRelationPageDataResponse;
import com.zbkj.common.response.UserStoreRelationDetailResponse;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
/**
* @author Caiyc
* @description UserStoreRelationServiceImpl 接口实现
* @date 2025-07-28
*/
@Service
public class UserStoreRelationServiceImpl extends ServiceImpl<UserStoreRelationDao, UserStoreRelation> implements UserStoreRelationService {

    @Resource
    private UserStoreRelationDao dao;

    @Override
    public Boolean create(UserStoreRelationSaveRequest userStoreRelationSaveRequest) {
        UserStoreRelation userStoreRelation = new UserStoreRelation();
        BeanUtils.copyProperties(userStoreRelationSaveRequest, userStoreRelation, "id");
        boolean save = save(userStoreRelation);
        if (!save) {
            throw new AppException(CommonResultCode.VALIDATE_FAILED, "添加失败");
        }
        return save;
    }

    /**
     * 分页查询用户店铺关系表
     *
     * @param request          请求参数
     * @param pageParamRequest 分页类参数
     * @return List<UserStoreRelation>
     * @author Caiyc
     * @since 2025-07-28
     */
    @Override
    public List<UserStoreRelationPageDataResponse> getList(UserStoreRelationSearchRequest request, PageParamRequest pageParamRequest) {
        PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());

        //带 UserStoreRelation 类的多条件查询
        LambdaQueryWrapper<UserStoreRelation> lambdaQueryWrapper = Wrappers.lambdaQuery();

        lambdaQueryWrapper.orderByDesc(UserStoreRelation::getCreateTime);
        List<UserStoreRelation> userStoreRelations = dao.selectList(lambdaQueryWrapper);
        // 添加活动状态
        List<UserStoreRelationPageDataResponse> userStoreRelationResponses = new ArrayList<>();

        userStoreRelations.stream().forEach(x->{
            UserStoreRelationPageDataResponse userStoreRelationResponse = new UserStoreRelationPageDataResponse();
            BeanUtils.copyProperties(x, userStoreRelationResponse);
            userStoreRelationResponses.add(userStoreRelationResponse);
        });
        return userStoreRelationResponses;
    }

    /**
    * 修改用户店铺关系表
    * @param request
    * @return
    */
    @Override
    public boolean update(UserStoreRelationSaveRequest request) {
        if (ObjectUtil.isNull(request.getId()) || request.getId() < 1) {
            throw new AppException(CommonResultCode.VALIDATE_FAILED, "请传入用户店铺关系表ID");
        }
        getByIdException(request.getId());
        UserStoreRelation userStoreRelation = new UserStoreRelation();
        BeanUtils.copyProperties(request, userStoreRelation);
        return updateById(userStoreRelation);
    }

    private UserStoreRelation getByIdException(Integer id) {
        UserStoreRelation userStoreRelation = getById(id);
        if (ObjectUtil.isNull(userStoreRelation)) {
            throw new AppException("用户店铺关系表不存在");
        }
        if (userStoreRelation.getIsDel()) {
            throw new AppException("用户店铺关系表不存在");
        }
        return userStoreRelation;
    }

    /**
    * 批量删除
    * @param idList
    * @return
    */
    @Override
    public boolean batchDelete(List<Integer> idList) {
    LambdaUpdateWrapper<UserStoreRelation> luw = Wrappers.lambdaUpdate();
        luw.set(UserStoreRelation::getIsDel, true);
        luw.in(UserStoreRelation::getId, idList);
        boolean update = update(luw);
        if (!update) {
            throw new AppException(CommonResultCode.ERROR.setMessage("批量删除用户店铺关系表失败"));
        }
        return true;
    }

    /**
    * 查询详情
    * @param id
    * @return
    */
    @Override
    public UserStoreRelationDetailResponse getDetails(Integer id) {
        LambdaQueryWrapper<UserStoreRelation> lqw = Wrappers.lambdaQuery();
        lqw.eq(UserStoreRelation::getIsDel, 0);
        lqw.last("limit 1");
        UserStoreRelation userStoreRelation = this.getOne(lqw);
        if (ObjectUtil.isNotNull(userStoreRelation)) {
            UserStoreRelationDetailResponse response = new UserStoreRelationDetailResponse();
            BeanUtils.copyProperties(userStoreRelation, response);
            return response;
        }
        return null;
    }

    @Override
    public UserStoreRelation getByUid(Integer uid) {
        LambdaQueryWrapper<UserStoreRelation> lqw = Wrappers.lambdaQuery();
        lqw.eq(UserStoreRelation::getUserId, uid);
        lqw.eq(UserStoreRelation::getIsDel, 0);
        lqw.orderByDesc(UserStoreRelation::getCreateTime);
        lqw.last("limit 1");
        return getOne(lqw);
    }

}

